﻿using LedProject.Logger;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace LedProject.Devices
{
    internal class LedDevice
    {
        //Led 的 ip 地址
        internal string ipStr { get; set; } 
        // Led 连接后的 TcpClient
        internal TcpClient tcpClient { get; set; } = new TcpClient();
        //Led 关灯
        internal void CloseLight()
        {
            SendMsg(new byte[] { 0x00, 0x01, 0 });
        }
        //亮度渐开
        internal void SlideOpenLight()
        {
            SendMsg(new byte[] { 0x00, 0x02, 0 });
        }
        //控制灯到一定的亮度
        internal void OpenLight(int value)
        {
            SendMsg(new byte[] { 0x00, 0x01, (byte)value });
        }
        //像wifi 传递数据
        internal void SendMsg(byte[] arr)
        {
            try
            {
                tcpClient.Client.Send(arr);
            }
            catch (Exception e)
            {
                //传输失败
                Log.Error(e);
                //移除传输失败led，等待再次连接
                App.Core.ledDevices.Remove(this);
            }
        }

        internal void OpenOrCloseLight(bool state)
        {
            if (state) SlideOpenLight();
            else CloseLight();
        }
    }
}
